// pagesB/question/question.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isActive: 1,
    subActive: 0,
    createActive: false,
    sortId: 0,
    sortIndex: 0,
    typeId: 0,
    typeIndex: 0,
    typeActive: 0,
    sort: [{
      value: '时间'
    }, {
      value: '题数'
    }],
    type: [{
      value: '全部'
    }, {
      value: '默认题库'
    }, {
      value: '公开题库'
    }],
    content: [],
    width: 0,
    left: 0,
    baseLeft: 500,
    isLike: 10,
  },
  getBank(e) {
    this.setData({
      isActive: e.currentTarget.dataset.id
    })
  },
  getSub(e) {
    const id = e.currentTarget.dataset.id
    this.setData({
      subActive: id,
      content: id == 1 ? this.data.sort : id == 2 ? this.data.type : '',
      typeActive: id == 1 ? this.data.sortIndex : id == 2 ? this.data.typeIndex : ''
    })
  },
  getType(e) {
    const sub = this.data.subActive
    const id = e.currentTarget.dataset.id
    this.setData({
      typeActive: id,
      typeIndex: sub == 2 ? id : this.data.typeIndex,
      sortIndex: sub == 1 ? id : this.data.sortIndex
    })
  },
  cancel() {
    let data = this.data
    this.setData({
      subActive: 0,
      typeIndex: data.typeId,
      sortIndex: data.sortId
    })
  },
  determine() {
    let data = this.data
    this.setData({
      subActive: 0,
      sortId: data.sortIndex,
      typeId: data.typeIndex
    })
  },
  showCreate() {
    const width = this.data.width
    const active = this.data.createActive
    const query = wx.createSelectorQuery()
    query.select('.created-button').boundingClientRect(res => {
      console.log(width, res.left);
      const l = width - res.left + 10
      if (l > 0 && !active) {
        this.setData({
          createActive: !active,
          left: l
        })
      } else {
        this.setData({
          createActive: !active,
          left: 0
        })
      }
    }).exec()
  },
  getLike(e) {
    const id = e.currentTarget.dataset.id
    if (this.data.isLike == id) {
      this.setData({
        isLike: 10
      })
    } else {
      this.setData({
        isLike: id
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (this.data.sortId != 0 || this.data.typeId != 0) {
      this.setData({
        typeIndex: typeId,
        sortIndex: sortId
      })
    }
    const query = wx.createSelectorQuery()
    query.select(".created-content").boundingClientRect(res => {
      this.setData({
        width: res.width
      })
    }).exec()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})